﻿/***************************************************
//  Copyright (c) Premium Tax Free 2011
***************************************************/

using System;
using System.Threading;
using PremierTaxFree.PTFLib.Extensions;

namespace PremierTaxFree.PTFLib.Threading
{
    public abstract class ThreadWorkerBase
    {
        private Thread m_Thread;
        public event ThreadExceptionEventHandler Error;
        public void Start(object data)
        {
            m_Thread = new Thread(ThreadFunction);
            m_Thread.IsBackground = true;
            m_Thread.Start(data);
            m_Thread = null;
        }

        public void Stop()
        {
            m_Thread.AbortSafe();
        }

        protected abstract void ThreadFunction(object data);
        protected virtual void FireError(Exception ex)
        {
            if (Error != null)
                Error(this, new ThreadExceptionEventArgs(ex));
        }
    }
}
